package com.omega_r.omegarecyclerview.sticky_header_example;

import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class StickyHeaderAbility extends Ability {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_sticky_header);

        List<StickyBean> stickyBeans = new ArrayList<>();
        for (int i = 0; i < 53; i++) {
            stickyBeans.add(new StickyBean("Item " + i, 0));
        }
        stickyBeans.add(0, new StickyBean("Header " + 0, 1));
        stickyBeans.add(7, new StickyBean("Header " + 1, 1));
        stickyBeans.add(14, new StickyBean("Header " + 2, 1));
        stickyBeans.add(21, new StickyBean("Header " + 3, 1));
        stickyBeans.add(28, new StickyBean("Header " + 4, 1));
        stickyBeans.add(35, new StickyBean("Header " + 5, 1));
        stickyBeans.add(42, new StickyBean("Header " + 6, 1));
        stickyBeans.add(49, new StickyBean("Header " + 7, 1));
        stickyBeans.add(56, new StickyBean("Header " + 8, 1));

        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_recycler_list);
        BaseProvider<StickyBean> provider = new BaseProvider(getContext(), listContainer, stickyBeans, ResourceTable.Layout_item_string);
        provider.setComponentBindingListener(new BaseProvider.ComponentBindingListener() {
            @Override
            public BaseViewHolder creatViewHolder(Component item) {
                return new StickyViewHolder(item);
            }

            @Override
            public void initData(BaseViewHolder baseViewHolder, Object object, int position) {
                ((StickyViewHolder) baseViewHolder).updateView((StickyBean) object);
            }
        });
        provider.setStickyListener(object -> ((StickyBean) object).getType() == 0 ? true : false);
        provider.setDividerListener(new BaseProvider.DividerListener() {
            @Override
            public Component createDivider() {
                return LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_item_sticky_divider, null, false);
            }
        });
        listContainer.setItemProvider(provider);
    }
}
